#include <iostream>
#include "Page.h"
#include "URLInputStream.h"
#include "CS240Exception.h"
#include "WebCrawler.h"

string stopwordLoc;
string outputLoc;
string startUrl;

int parseArguments(int argc, char *argv[] ){
	// There should be 2 arguments, the absolute and relativeurl
	//argc = 4;
	//argv[1] = "http://students.cs.byu.edu/~cs240ta/crawler_tests/crawlindex.html";
	//argv[2] = "output.xml" ;
	//argv[3] = "stopwords.txt";
	if(argc != 4 ) {
		printf("Usage: ./crawler <start-url> <output-file> <stopword-file>\n");
		return(-1);
	}
	startUrl = argv[1];
	outputLoc = argv[2];
	stopwordLoc = argv[3];
	return 0;
}
int main (int argc,char *argv[])
{
	parseArguments(argc,argv);
	try{

			WebCrawler * wc = new WebCrawler(startUrl);
			wc->loadStopWords(stopwordLoc);
			wc->beginCrawl();
			wc->printXml(outputLoc);
			delete(wc);

	}catch (std::exception &e){
		std::cout << "Exception Occurred:" << e.what() << std::endl;
	}
	catch (CS240Exception &e){
		std::cout << "Exception Occurred:" << e.GetMessage() << std::endl;
	}
	catch (...){
		std::cout << "Unknown Exception Occurred" << std::endl;
	}
	return 0;
}
